package org.linlinjava.litemall.admin.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.linlinjava.litemall.db.domain.LitemallBill;
import org.springframework.stereotype.Service;

import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: 导出平台结算单的信息
 * @date: 2019/12/12
 * @description:
 */
@Service
public class BillReportService {
    private final Log logger = LogFactory.getLog(BillReportService.class);

    /**
     * 生成Excel表格。循环插入..部署时候需要：：更改地址
     *
     * @param json
     */

    public String createExcel(String json) {

        JSONObject jsonObject = JSONObject.parseObject(json);
        JSONArray bills = (JSONArray) jsonObject.get("bills");

        logger.info("读取到JSON入参bills需要生成报表>>>>>>>>>>>>" + bills);
        //创建工作簿
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建sheet表单
        HSSFSheet sheet = wb.createSheet("本期结算单");
        //创建第一行数据，用于放title栏内容
        Row titleRow = sheet.createRow(0);
        titleRow.createCell(0).setCellValue("商户名");
        titleRow.createCell(2).setCellValue("订单编号");
        titleRow.createCell(4).setCellValue("订单确认时间");
        titleRow.createCell(6).setCellValue("结算日期");
        titleRow.createCell(8).setCellValue("原定金额");

        titleRow.createCell(10).setCellValue("平台优惠");
        titleRow.createCell(12).setCellValue("商户优惠");
        titleRow.createCell(14).setCellValue("向商户开票金额");
        titleRow.createCell(16).setCellValue("实际支付");
        titleRow.createCell(18).setCellValue("商户收入");

        titleRow.createCell(20).setCellValue("中介邀请码");
        titleRow.createCell(22).setCellValue("中介收入");
        titleRow.createCell(24).setCellValue("平台收入");

        for (int i = 0; i < bills.size(); i++) {
          //  LitemallBill bill = bills.get(i);
         /*   String brandName = bill.getBrandName();
            String orderSn = bill.getOrderSn();
            LocalDateTime orderConfirmTime = bill.getOrderConfirmTime();
            LocalDate addTime = bill.getAddTime();
            BigDecimal income = bill.getIncome();

            BigDecimal brandIncome = bill.getBrandIncome();
            BigDecimal scalperPrice = bill.getScalperPrice();
            BigDecimal orderPrice = bill.getOrderPrice();
            BigDecimal invoicePrice = bill.getInvoicePrice();
            BigDecimal brandCoupon = bill.getBrandCoupon();
            BigDecimal actualPrice = bill.getActualPrice();
            BigDecimal couponPrice = bill.getCouponPrice();
            String customerInvitation = bill.getCustomerInvitation();*/

             JSONObject obj = (JSONObject) bills.get(i);
            Object brandName = obj.get("brandName");
            Object orderSn = obj.get("orderSn");
            Object orderConfirmTime = obj.get("orderConfirmTime");

            Object addTime = obj.get("addTime");
            Object income = obj.get("income");
            Object brandIncome = obj.get("brandIncome");

            Object scalperPrice = obj.get("scalperPrice");
            Object orderPrice = obj.get("orderPrice");
            Object invoicePrice = obj.get("invoicePrice");

            Object brandCoupon = obj.get("brandCoupon");
            Object actualPrice = obj.get("actualPrice");
            Object couponPrice = obj.get("couponPrice");

            Object customerInvitation = obj.get("CustomerInvitation");
            if(null == customerInvitation){ customerInvitation="无";}
            //创建从第二行开始的数据
            Row billRow = sheet.createRow(i + 1);
            //1brandName商户名称0
            if (brandName != null) {
                billRow.createCell(0).setCellValue(brandName.toString());
            } else {
                billRow.createCell(0).setCellValue("");
            }
            //2orderSn订单编号2
            if (orderSn != null) {
                billRow.createCell(2).setCellValue(orderSn.toString());
            } else {
                billRow.createCell(2).setCellValue("");
            }
            //3orderConfirmTimes订单确认时间4
            if (orderConfirmTime != null) {
                billRow.createCell(4).setCellValue(orderConfirmTime.toString());
            } else {
                billRow.createCell(4).setCellValue("");
            }
            //4addTime结算日期6
            if (addTime != null) {
                billRow.createCell(6).setCellValue(addTime.toString());
            } else {
                billRow.createCell(6).setCellValue("");
            }
            //5orderPrice原定金额8
            if (orderPrice != null) {
                billRow.createCell(8).setCellValue(orderPrice.toString());
            } else {
                billRow.createCell(8).setCellValue("");
            }
            //6couponPrice平台优惠
            if (couponPrice != null) {
                billRow.createCell(10).setCellValue(couponPrice.toString());
            } else {
                billRow.createCell(10).setCellValue("");
            }
            //7商户优惠12
            if (brandCoupon != null) {
                billRow.createCell(12).setCellValue(brandCoupon.toString());
            } else {
                billRow.createCell(12).setCellValue("");
            }
            //8向商户开票金额14
            if (invoicePrice != null) {
                billRow.createCell(14).setCellValue(invoicePrice.toString());
            } else {
                billRow.createCell(14).setCellValue("");
            }
            //9实际支付16
            if (actualPrice != null) {
                billRow.createCell(16).setCellValue(actualPrice.toString());
            } else {
                billRow.createCell(16).setCellValue("");
            }

            //10brandIncome商户收入
            if (brandIncome != null) {
                billRow.createCell(18).setCellValue(brandIncome.toString());
            } else {
                billRow.createCell(18).setCellValue("");
            }
            //11中介CustomerInvitation 20
            if (customerInvitation != null) {
                billRow.createCell(20).setCellValue(customerInvitation.toString());
            } else {
                billRow.createCell(20).setCellValue("");
            }
            //12中介收入22
            if (scalperPrice != null) {
                billRow.createCell(22).setCellValue(scalperPrice.toString());
            } else {
                billRow.createCell(22).setCellValue("");
            }
            //13income平台收入24
            if (income != null) {
                billRow.createCell(24).setCellValue(income.toString());
            } else {
                billRow.createCell(24).setCellValue("");
            }
        }
    //如果之前位置有文件----删除。
        //获取本地桌面地址 savePath
      /*  FileSystemView fsv = FileSystemView.getFileSystemView();
        File home = fsv.getHomeDirectory();
        String savePath = home.getPath();*/
      //获取服务部署路径
        String savePath = System.getProperty("user.dir") + "\\bill";
        String fileName ="\\billReport.xls";
        File file= new File(savePath + fileName);
        //文件目录不存在创建目录
        File fileParent = file.getParentFile();
        System.out.println("项目路径:结算单文件夹>>>" + fileParent);
        if(!fileParent.exists()){
            fileParent.mkdirs();
        }
        if(file.exists()){
            file.delete();
        }
            try {
                FileOutputStream fout = new FileOutputStream(file);
                wb.write(fout);
                fout.close();
                //创建文件容易造成1KB或0KB
              //   file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            String path = file.getPath();
        System.out.println("文件保存路径>>>" + path );
        return path;
    }
}